/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package officeboost.domain;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import officeboost.entity.User;
import officeboost.entity.Group;

/**
 *
 * @author sorra
 */
@Stateless
@LocalBean
public class GroupService {

    @PersistenceContext
    EntityManager em;
    @EJB
    LogMgr logger;
    @EJB
    MailService mail;


    public void testSetup() {


        Group g1 = new Group();
        g1.setName("Student");

        persist(g1);
        Group g2 = new Group();
        g2.setName("Worker");
        persist(g2);
        Group g3 = new Group();
        g3.setName("Admin");
        persist(g3);
        logger.log("group persisted");
    }

    public void assign(User user, Group group) {
        group.getUsers().add(user);
        em.merge(group);
    }

    public void persist(Group group) {
        if (group == null) {
            return;            
        }
        em.persist(group);
    }

    public Group find(long id) {
        return em.find(Group.class, id);
    }

    // 按组查找
    public List<User> findUsersByGroup(Group group) {
        if (group == null) {
            return null;
        }
        return group.getUsers();
    }

    public List<Group> findAll() {
        Query query = em.createQuery("from Group g");
        List<Group> groups = query.getResultList();
        if (groups == null) {
            groups = new ArrayList<>();
        }
        return groups;
    }

    // 由组名得到组
    public Group getGroupByName(String groupName) {
        Query query = em.createQuery("from Group g where g.name='" + groupName + "'");
        List<Group> groups = query.getResultList();
        // 若不存在返回null
        if (groups.isEmpty()) {
            return null;
        }
        // 假定组名唯一
        return groups.get(0);
    }

    // 由组名得到组内用户
    public List<User> getUsersByGruopName(String groupName) {
        Group group = getGroupByName(groupName);
        if (group == null) {
            return null;
        }
        return group.getUsers();
    }

     // 将用户加入到某组
    public void addUserToGroup(String id, String groupName) {
        Group group = getGroupByName(groupName);
        if (group == null) {
            return;
        }
//        PersonService ps = new PersonService();
        List<User> users = group.getUsers();
        boolean isExist = false;
        Iterator it = users.iterator();
        while (it.hasNext()) {
            User user = (User) it.next();
            if (user.equals(em.find(User.class, id))) {
                isExist = true;
            }
        }
        if (!isExist) {
            User user = em.find(User.class, id);
//            String email = user.getEmail();
            String email = "574783874@qq.com";
            String theme = "用户权限更改";
            String detail = "您添加了" + groupName + "权限！";
            users.add(user);
            group.setUsers(users);
            em.merge(group);
            mail.sentMail(email, theme, detail);
//            ObMailService om = new ObMailService();
//            om.sentMail(email, theme, detail);
        }

    }

    // 将用户从某组中删除
    public void deleteUserFromGroup(String id, String groupName) {
        Group group = getGroupByName(groupName);
        if (group == null) {
            return;
        }
//        PersonService ps = new PersonService();
        List<User> users = group.getUsers();
        Iterator it = users.iterator();
        while (it.hasNext()) {
            User user = (User) it.next();
            User delete = em.find(User.class, id);
            if (user.equals(delete)) {
//                String email = delete.getEmail();
                String email = "574783874@qq.com";
                String theme = "用户权限更改";
                String detail = "您被取消了" + groupName + "权限！";
                it.remove();
//                mail.sentMail(email, theme, detail);
//                ObMailService om = new ObMailService();
//                om.sentMail(email, theme, detail);
            }
        }
        group.setUsers(users);
        em.merge(group);
    }

}
